<?php
/**
* @package acl
* @filesource
*/

/** 
* 类名:acl
* 
* 描述:本class作用是权限管理和用户管理的全部操作
*
* @author  junfeng.song <junfeng.song@square-enix.net.cn>
* @access  public
* @copyright square-enix
* @version CVS: $Id: aclController.php 2007/10/10 19:48:47  junfeng.song $
* @package aclController
* @link       http://www.square-enix.net.cn/
*/
class AclController extends MyClass_Action{
    
	public function init()
    {
		$this->_user = $this->checkLogin ();
		/* Initialize action controller here */
		$this->controller = $this->getRequest ()->getControllerName ();
		$this->action = $this->getRequest ()->getActionName ();
		
		$this->commonTemplates ();
		
		$this->_smarty->assign ( 'controller', $this->controller );
		$this->_smarty->assign ( 'action', $this->action );
		$this->_smarty->assign ( '_user', $this->_user );

		$this->_smarty->assign ( 'location_file', $this->controller.'/location.tpl' );		
		
		//get database
		$this->getDb ( 'osmdb' );
    }  
	/**
	 * 全部的资源和其下属操作的列表显示函数----------------------------------
	 */
	public function resource_list() {
		
		/**
		 * 为新增加的填写表单操作查询全部的资源操作
		 */
		$select = $this->_dbAdapter->select ();
		$select->from ( 'crm_staff_doresource', '*' );
		$sql = $select->__toString ();
		$result_doresource = $this->_dbAdapter->fetchAll ( $sql );
		/**
		 * 全部的资源
		 */
		$select = $this->_dbAdapter->select ();
		$select->from ( 'crm_staff_resource', '*' )->order ( 'our_group' );
		$sql = $select->__toString ();
		$result_resource = $this->_dbAdapter->fetchAll ( $sql );
		/**
		 * 资源和资源关联数组
		 */
		$select = $this->_dbAdapter->select ();
		$select->from ( 'crm_staff_resource_doresource', '*' );
		$select->join ( 'crm_staff_doresource', 'crm_staff_resource_doresource.oud_id = crm_staff_doresource.oud_id', 'oud_zh_name' );
		$sql = $select->__toString ();
		
		$result_resource_doresource = $this->_dbAdapter->fetchAll ( $sql );
		while ( list ( $key, $value ) = each ( $result_resource_doresource ) ) {
			while ( list ( $key_a, $value_a ) = each ( $result_resource ) ) {
				if ($value_a ['our_id'] == $value ['our_id']) {
					if (! isset ( $result_resource [$key_a] ['do'] )) {
						$result_resource [$key_a] ['do'] = array ();
						$result_resource [$key_a] ['oud_id_do'] = array ();
						$result_resource [$key_a] ['do_sign'] = array ();
					}
					//操作中文提示
					$result_resource [$key_a] ['do'] [$value ['oud_id']] = $value ['oud_zh_name'];
					//操作id
					$result_resource [$key_a] ['oud_id_do'] [$value ['oud_id']] = $value ['oud_id'];
					//操作英文提示
					$result_resource [$key_a] ['do_sign'] [$value ['oud_id']] = $value ['ourd_sign'];
				}
			}
			reset ( $result_resource );
		}
		/**
		 * 返回整合后的资源数组,返回资源的操作类型数组
		 */
		return array ('result_resource' => $result_resource, 'result_doresource' => $result_doresource );
	
	}
	/**
	 * 一个用户组的权限列表,参数:用户组id--------------------------
	 */
	public function group_list($info) {
		$result_array = $this->resource_list ();
		$result_resource = $result_array ['result_resource'];
		$result_doresource = $result_array ['result_doresource'];
		/**
		 * 该用户组的全部权限
		 */
		$select = $this->_dbAdapter->select ();
		$select->from ( 'crm_staff_group_access', '*' )->where ( 'oug_id = ?', $info );
		$sql_access = $select->__toString ();
		$result_group_access = $this->_dbAdapter->fetchAll ( $sql_access );
		$access = array ();
		while ( list ( $key, $value ) = each ( $result_group_access ) ) {
			array_push ( $access, $value ['ourd_sign'] );
		}
		/**
		 * 修改时的复选框
		 */
		while ( list ( $key, $value ) = each ( $result_resource ) ) {
			$zhi = '0';
			$result_resource [$key] ['checked'] = array ();
			while ( list ( $key_r, $value_r ) = each ( $result_resource [$key] ['do_sign'] ) ) {
				if (in_array ( $value_r, $access )) {
					$result_resource [$key] ['checked'] [$key_r] = '1';
					$zhi = $zhi + 1;
				} else {
					$result_resource [$key] ['checked'] [$key_r] = '0';
					$zhi = $zhi;
				}
			}
			if ($zhi != '0') {
				$result_resource [$key] ['now'] = '1';
			}
		}
		return $result_resource;
	}
	/**
	 * 用户组管理首页--------------------------------------------------
	 */
	public function groupAction() {
		try {
			
			
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
	
		} catch ( Exception $e ) {
			echo $e->getMessage ();
		}
	}
	
	/**
	 * 权限管理首页默认是用户管理首页-------------------------------------------
	 */
	public function indexAction() {
		try {

			$count = 40; //pre
			$curpage = ( int ) $this->_request->getParam ( 'page' );
			if (! $curpage)
				$curpage = 1;
			$offset = ($curpage - 1) * $count;
			
			$sql = 'select SQL_CALC_FOUND_ROWS a.*,b.shop_name,c.position 
			from crm_staff a 
			left join  crm_shops b on a.shopid=b.shopid 
			left join  crm_position c on a.positionid=c.positionid 
			order by a.`isdisabled` asc,a.uid asc ' . " limit $offset,$count";
			$result = $this->_dbAdapter->fetchAssoc ( $sql );
			//print_r($result);
			foreach ( $result as $key => $value ) {
				$result [$key] ['lasttime'] = $value ['lasttime'] ? date ( 'Y-m-d H:i', $value ['lasttime'] ) : '';
				$result [$key] ['sexDisp'] = $value ['sex'] ? '女' : '男';
			}
			
			//翻页控制
			$param ['file'] = '/' . $this->controller . '/' . $this->action;
			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' );
			$param ['perpagenum'] = $count; // 每页显示的数目
			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
			$param ['curpage'] = $curpage; // 当前页码
			$this->_smarty->assign ( 'allnum', $param ['totalnum'] );
			
			//	require_once 'MyClass/class.page.php';
			$page = new MyClass_Page ( $param );
			//$page->setvar ( array ('gid' => $gradeid,'typeid'=>$typeid) );
			

			$this->_smarty->assign ( 'page', $page->getNumPage () );
			$this->_smarty->assign ( 'result', $result );
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
		
		} catch ( Exception $e ) {
			echo $e->getMessage ();
		}
	}
	/**
	 * -------------------------------------------------------------------------------------------------------------------
	 * 资源管理
	 */
	public function resourceAction() {
		try {

			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );

		} catch ( Exception $e ) {
			echo $e->getMessage ();
		}
	}
	
	/**
	 *-----------------------------------------------------------------------------------------------------------------------------------------------
	 * 资源下属操作管理
	 */
	public function doresourceAction() {
		try {
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
		} catch ( Exception $e ) {
			echo $e->getMessage ();
		}
	}
	
	
	
	//reserve statement as below
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		
		throw new Exception ( 'Invalid method' );
		
		// all other methods throw an exception
		throw new Exception ( 'Invalid method "' . $method . '" called' );
	}
}
